package com.student_exam_system.service.impl;

import com.student_exam_system.Util.CollectionSort;
import com.student_exam_system.Util.MybatisUtils;
import com.student_exam_system.dao.AnswerMapper;
import com.student_exam_system.pojo.Answer;
import com.student_exam_system.pojo.AnswerCards;
import com.student_exam_system.service.AnswerService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Objects;
/**
 * @author fire'ge
 */
@Service("answerServiceImpl")
public class AnswerServiceImpl implements AnswerService {
    final
    AnswerMapper answerMapper;

    public AnswerServiceImpl(AnswerMapper answerMapper) {
        this.answerMapper = answerMapper;
    }

    public AnswerMapper getAnswerMapper() {
        return answerMapper;
    }


    @Override
    public int addAnswer(Answer answer) {
        return answerMapper.addAnswer(answer);
    }

    @Override
    public int addMulAnswer(List<Answer> answers) {
        return answerMapper.addMulAnswer(answers);
    }
    @Override
    public List<AnswerCards> queryAnswerCard(int userId, int paperId){
        return answerMapper.queryAnswerCard(userId,paperId);
    }

    private final SqlSession session = MybatisUtils.getSession();
    private final CollectionSort collectionSort = new CollectionSort();
//    private final String number = "^[1-9]\\d*|0$";

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Answer> answerList(int paperId, int questionId, int teacherId) {
        List<Answer> answers =null;
        try {
            answers = answerMapper.markQuestion(paperId, questionId, teacherId);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            MybatisUtils.closeSession(session);
        }
        return answers;
    }


    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public int giveMark(int choice,double point,int studentId,int paperId,int questionId) {
        //输入的分数匹配0+正整数
        if (point <0) {
            return 0;
        }
        int num;
        try {
            if (choice == 1) {
                //手改
                num= answerMapper.givePoint(paperId, studentId, questionId, point);
                num=num+ answerMapper.updateQaStatus(paperId, questionId, studentId);
                int i = collectionSort.num(studentId, paperId);
                num = num + i;
                return num;
            }else if (choice == 2) {
                //自动改
                //标准答案
                int j;
                String solution = answerMapper.getSolution(paperId, questionId);
                String answer = answerMapper.getAnswer(paperId, studentId, questionId);
                if (Objects.equals(solution, answer)){
                    j = answerMapper.givePoint(paperId, studentId, questionId, 5);
                }else {
                    j =answerMapper.givePoint(paperId,studentId,questionId,0);
                }
                int i = collectionSort.num(studentId, paperId);
                return i+j;
            }else {
                return -1;
            }
        }catch (Exception e) {
            e.printStackTrace();
            //手动提交事务回滚

        }finally {
            MybatisUtils.closeSession(session);
        }
        return 0;
    }
}
